package cn.com.yiqi.wsc.portal.controller.function;

import cn.com.yiqi.wsc.common.BaseCriteria;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.entity.func.Function;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.portal.controller.admin.ro.FuncTreeRO;
import cn.com.yiqi.wsc.service.func.dataobject.FuncTreeNodeDO;
import cn.com.yiqi.wsc.service.function.FunctionService;
import cn.com.yiqi.wsc.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 苟宇恒 on 2017/2/23.
 * 功能管理
 */
@Controller
@RequestMapping("/function")
public class FunctionController extends BaseController {

    @Autowired
    private FunctionService functionService;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("function/index");
        this.getUserData(mv);
        return mv;
    }

    /**
     * 查询列表
     *
     * @param criteria
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result pageList(@RequestBody BaseCriteria criteria) {
        Result result = new Result();

        if (criteria.getIsAll() == 1) {
            //获取所有的不分页
            result = functionService.getAll(criteria);
        } else {
            result = functionService.getPageList(criteria);
        }

        return result;
    }

    /**
     * 添加
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result insert(@RequestBody Function function) {
        Result result = new Result();

        function.setInsertUserId(userService.getCurrentUserId());

        result = functionService.insert(function);

        return result;
    }

    /**
     * 修改
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestBody Function function) {
        Result result = new Result();

        if (function.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        
        function.setUpdateUserId(userService.getCurrentUserId());

        result = functionService.update(function);

        return result;
    }

    /**
     * 删除
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestBody Function function) {
        Result result = new Result();

        if (function.getId() == null) {
            result.setErrMsg("参数错误");
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            return result;
        }

        result = functionService.delete(function);

        return result;
    }
}
